<?php
/*** 
 *
 *
 *
 **/
require_once 'db/db.php';
require_once 'util/perm.php';
require_once 'util/msg.php';

function deleteForum( $fid ) { 
  if( !hasPerm('delete.forum') ) 
    return MSG_UNAUTHORIZED;
 

  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();
 
    // authenticate fid

    $db->beginTransaction();
    $db->exec("DELETE FROM forum WHERE forum.id = $fid");
    $db->exec("DELETE FROM post WHERE post.tid = (SELECT thread.id FROM thread WHERE thread.fid = $fid)");
    $db->exec("DELETE FROM thread WHERE thread.fid = $fid");
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  }
};
function newForum( $title, $datum ) {
  if( !hasPerm('edit.thread') || !hasPerm('new.thread') ) 
    return MSG_UNAUTHORIZED;

  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();

    $title = $db->quote($title);
    $datum = $db->quote($datum);

    $db->beginTransaction();
    $db->exec("INSERT INTO forum (rank, title, datum)
                                  VALUES( 1 + (SELECT MAX(id) FROM forum),
                                          $title,
                                          $datum);");
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  } };
function setForum( $fid, $title, $datum, $rank ) {
  if( !hasPerm('edit.thread') || !hasPerm('new.thread') ) 
    return MSG_UNAUTHORIZED;

  $GLOBALS['__QNUM']++;
  try {
    $db = openDbConn();

    $rank= $db->quote($rank);
    $fid = $db->quote($fid);
    $title = $db->quote($title);
    $datum = $db->quote($datum);

    $db->beginTransaction();
    $db->exec("UPDATE forum
                  SET datum = $datum, title = $title
                    WHERE id = $fid;");
    $db->commit();
  } catch ( PDOException $e ) {
    $db->rollback();
    print "ERROR: " . $e->getMessage() . "<br />";
    die();
  } 
};

?>
